/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.internal.model;

import net.sourceforge.perforce.core.api.IPerforceServer;
import net.sourceforge.perforce.core.resources.IChangelist;
import net.sourceforge.perforce.ui.IPerforceUIConstants;
import net.sourceforge.perforce.ui.PerforceUIPlugin;
import net.sourceforge.perforce.ui.internal.Policy;
import net.sourceforge.perforce.ui.model.PerforceModelElement;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.model.IWorkbenchAdapter;

import java.util.Comparator;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public class OtherPendingChangelistContainer extends PerforceModelElement implements
    IWorkbenchAdapter, IAdaptable {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the Perforce Server. */
  private IPerforceServer server;
  /** Stores the Pending Changelist cache. */
  private PendingChangelistCache pendingChangelistCache;

  // -------------------------------------------------------------------------
  // Public Constructors

  /**
   * Constructor for OtherPendingChangelistContainer.
   * 
   * @param server reference to the Perforce server.
   * @param cache the pending changelist cache.
   */
  public OtherPendingChangelistContainer(IPerforceServer server, PendingChangelistCache cache) {
    this.server = server;
    pendingChangelistCache = cache;
  }

  // ---------------------------------------------------------- Public Methods
  // (IWorkbenchAdapter)

  public Object[] getChildren(Object o) {
    if (!(o instanceof OtherPendingChangelistContainer)) {
      return new Object[0];
    }

    IChangelist[] changelists =
        pendingChangelistCache.getChangelistElements(new Comparator<IChangelist>() {
          public int compare(IChangelist o1, IChangelist o2) {
            return 0;
          }

          @Override
          public boolean equals(Object changelist) {
            return !((IChangelist) changelist).getClientName().equals(server.getClient());
          }
        });

    return changelists;

  }

  public ImageDescriptor getImageDescriptor(Object o) {
    if (!(o instanceof OtherPendingChangelistContainer)) {
      return null;
    }

    String imageId = IPerforceUIConstants.IMG_OTHER_CHANGELISTS;
    return PerforceUIPlugin.getPlugin().getImageDescriptor(imageId);
  }

  public String getLabel(Object o) {
    if (!(o instanceof OtherPendingChangelistContainer)) {
      return null;
    }

    return Policy.bind("OtherPendingChangelistContainer.getLabel"); //$NON-NLS-1$
  }

  public Object getParent(Object o) {
    return null;
  }

  /**
   * Called to retrieve the perforce server.
   * 
   * @return the server.
   */
  public IPerforceServer getServer() {
    return server;
  }

  // ----------------------------------------------------------------- Public
  // methods (IAdaptable)

  @SuppressWarnings("unchecked")
  // adapter
  public Object getAdapter(Class adapter) {
    if (adapter == IWorkbenchAdapter.class) {
      return this;
    } else if (adapter == IPerforceServer.class) {
      return getServer();
    }

    return null;
  }
}
